﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using ByteSite.Common;

namespace ByteSite.BLL
{
    /// <summary>
    ///用户随机码表
    /// </summary>
    public partial class user_code
    {
        private readonly Entitys.sysconfig sysConfig = new BLL.sysconfig().loadConfig();//获得系统配置信息
        private readonly DAL.Data data;

        public user_code()
        {
            data = new DAL.Data();
        }

        #region 基本方法================================
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            return data.Exists<Entitys.dt_user_code>(id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Entitys.dt_user_code model)
        {
            return data.Insert(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Entitys.dt_user_code model)
        {
            return data.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            return data.Delete<Entitys.dt_user_code>(id);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Entitys.dt_user_code GetModel(int id)
        {
            return data.Get<Entitys.dt_user_code>(id);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Entitys.dt_user_code> GetList(string strWhere)
        {
            return data.List<Entitys.dt_user_code>(strWhere);
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public List<Entitys.dt_user_code> GetList(int Top, string strWhere, string filedOrder)
        {
            return data.List<Entitys.dt_user_code>(Top, strWhere, filedOrder);
        }

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public List<Entitys.dt_user_code> GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            return data.List<Entitys.dt_user_code>(pageSize, pageIndex, strWhere, filedOrder, out recordCount);
        }
        #endregion

        #region 扩展方法================================
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string type, string user_name)
        {
            return data.Exists<Entitys.dt_user_code>(x => x.type == type && x.user_name == user_name);
        }

        /// <summary>
        /// 根据条件批量删除
        /// </summary>
        public int Delete(Expression<Func<Entitys.dt_user_code, bool>> whereExpression)
        {
            return data.Delete<Entitys.dt_user_code>(whereExpression);
        }

        /// <summary>
        /// 根据生成码得到一个对象实体
        /// </summary>
        public Entitys.dt_user_code GetModel(string str_code)
        {
            return data.Get<Entitys.dt_user_code>(x => x.str_code == str_code);
        }

        /// <summary>
        /// 根据用户名得到一个对象实体
        /// </summary>
        /// <param name="user_name">用户名</param>
        /// <param name="code_type">验证码类型</param>
        /// <param name="datepart">日期格式,d(天)hh(小时)n(分钟)s秒</param>
        /// <returns></returns>
        public Entitys.dt_user_code GetModel(string user_name, string code_type, string datepart)
        {
            return data.Get<Entitys.dt_user_code>(x => x.status == 0 && x.user_name == user_name && x.type == code_type && x.eff_time > DateTime.Parse(DateTime.Now.ToString("yyyy/MM/dd") + " 00:00:00") && x.eff_time < DateTime.Parse(DateTime.Now.AddDays(1).ToString("yyyy/MM/dd") + " 00:00:00"));
        }

        /// <summary>
        /// 返回数据总数
        /// </summary>
        public int GetCount(Expression<Func<Entitys.dt_user_code, bool>> whereExpression)
        {
            return data.Count<Entitys.dt_user_code>(whereExpression);
        }
        #endregion
    }
}